package com.ruoyi.psi.sale.salereturn.service;


import com.ruoyi.DingDing.Domain.DingDingText;
import com.ruoyi.constant.ERPConstant;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.personnel.oa.approve.domain.ApproveDomain;
import com.ruoyi.personnel.oa.approve.mapper.ApproveMapper;
import com.ruoyi.personnel.oa.sequence.service.IApproveSequenceService;
import com.ruoyi.psi.base.warehouse.domain.WarehouseListDomain;
import com.ruoyi.psi.base.warehouse.domain.WarehouseListDto;
import com.ruoyi.psi.base.warehouse.mapper.WarehouseMapper;
import com.ruoyi.psi.sale.salereturn.domain.SaleReturn;
import com.ruoyi.psi.sale.salereturn.domain.SaleReturnList;
import com.ruoyi.psi.sale.salereturn.mapper.SaleReturnMapper;
import com.ruoyi.utils.ID.IdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * 销售退回
 * 业务执行层
 * 修改：lsy 2021.8.19
 */
@Service
public class SaleReturnServiceImpl implements ISaleReturnService {

    @Autowired
    private SaleReturnMapper saleReturnMapper;
    @Autowired
    private WarehouseMapper warehouseMapper; //仓库
    @Autowired
    private IdUtil idUtil;
    @Autowired
    private IApproveSequenceService approveSequenceService;
    @Autowired
    private ApproveMapper approveMapper;

    /**
     * 销售订单退货查询
     *
     * @param saleReturn
     * @return
     */
    @Override
    public List<SaleReturn> selectSaleReturnList(SaleReturn saleReturn) {
        List<SaleReturn> saleReturns = saleReturnMapper.selectSaleReturnList(saleReturn);
        for (int i = 0; i < saleReturns.size(); i++) {
            if (StringUtils.isNotNull(saleReturnMapper.selectSaleReturnListListById(saleReturns.get(i).getSaleReturnId()))) {
                //存在，则存入销售订单退货对应产品信息
                saleReturns.get(i).setSaleReturnLists(saleReturnMapper.selectSaleReturnListListById(saleReturns.get(i).getSaleReturnId()));
            }
        }
        return saleReturns;
    }

    /**
     * 按编号查询
     *
     * @param saleReturnId 销售订单退货编号
     * @return
     */
    @Override
    public SaleReturn selectSaleReturnById(String saleReturnId) {
        return saleReturnMapper.selectSaleReturnById(saleReturnId);
    }

    /**
     * 销售订单退货新增
     *
     * @param saleReturn 销售入库单据
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertSaleReturn(SaleReturn saleReturn) throws Exception {
        String saleReturnId = idUtil.createSaleReturnId(); //自动生成编号
        if (StringUtils.isNotNull(saleReturnMapper.selectSaleReturnById(saleReturnId))) {
            //存在
            return 0;
        }
        //不存在
        saleReturn.setSaleReturnId(saleReturnId); // 主表添加编号
        ApproveDomain approve = new ApproveDomain(saleReturnId, "销售退货审批", ERPConstant.APPROVAL_TYPE_SALE_RETURN, saleReturn.getStaffId()); //新增审批
        approve.setCurrentApprover(approveSequenceService.getLowestRoleId(ERPConstant.APPROVAL_TYPE_SALE_RETURN));
        approveMapper.insertApprove(approve);
        Long currentApprover = approve.getCurrentApprover();
        DingDingText dingDingText = new DingDingText();
        dingDingText.BatchRobotText(currentApprover);
        saleReturnMapper.insertSaleReturn(saleReturn);//销售订单退货据
//        List<WarehouseListDto> warehouseListDtos = warehouseMapper.selectWarehouseListListById(saleReturn.getWarehouseId()); // 查询该仓库的产品列表
        List<SaleReturnList> saleReturnLists = saleReturn.getSaleReturnLists();//销售订单退货产品信息
        for (int i = 0; i < saleReturnLists.size(); i++) {
            saleReturnLists.get(i).setSaleReturnId(saleReturnId); // 子表添加编号
//            WarehouseListDomain warehouseListDomain = new WarehouseListDomain(); // 仓库子表的对象
//            warehouseListDomain.setWarehouseId(saleReturn.getWarehouseId());
//            warehouseListDomain.setProduceId(saleReturnLists.get(i).getProduceId());
//            boolean isExistFlag = false;
//            for (WarehouseListDto s : warehouseListDtos) {
//                // 遍历仓库
//                if (saleReturnLists.get(i).getProduceId().equals(s.getProduceId())) {
//                    // 该产品存在，更新产品数量，产品数量 = 原库存数 + 该产品入库的数量
//                    warehouseListDomain.setProductNumber(s.getProductNumber() + saleReturnLists.get(i).getSaleReturnNumber());
//                    isExistFlag = true;
//                    break; //终止循环
//                }
//            }
//            if (isExistFlag) {
//                // 该产品存在，更新库存
//                warehouseMapper.updateWarehouseList(warehouseListDomain);
//            } else {
//                // 该产品不存在，在库存中新增产品
//                warehouseListDomain.setProductNumber(saleReturnLists.get(i).getSaleReturnNumber());
//                warehouseMapper.insertWarehouseList(warehouseListDomain);
//            }
            saleReturnMapper.insertSaleReturnList(saleReturnLists.get(i));//销售订单退货产品信息
        }
        return 1;
    }

    /**
     * 销售订单退货更新
     *
     * @param saleReturn 销售入库单据
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateSaleReturn(SaleReturn saleReturn) throws Exception {
        if (!StringUtils.isNotNull(saleReturnMapper.selectSaleReturnById(saleReturn.getSaleReturnId()))) {
            //不存在
            return 0;
        }
        //存在销售订单退货单据
        saleReturnMapper.updateSaleReturn(saleReturn);//销售订单退货单据
        List<SaleReturnList> saleReturnListOld = saleReturnMapper.selectSaleReturnListListById(saleReturn.getSaleReturnId());
        List<SaleReturnList> saleReturnListNew = saleReturn.getSaleReturnLists();//销售订单退货产品信息
//        List<WarehouseListDto> warehouseListDtos = warehouseMapper.selectWarehouseListListById(saleReturn.getWarehouseId()); // 查询该仓库的产品列表
        for (int i = 0; i < saleReturnListNew.size(); i++) {
            SaleReturnList saleReturnList = saleReturnMapper.selectSaleReturnListById(saleReturn.getSaleReturnId(), saleReturnListNew.get(i).getProduceId());
            if (StringUtils.isNotNull(saleReturnList)) {
                for (int j = 0; j < saleReturnListOld.size(); j++) {
                    if (saleReturnListOld.get(j).getProduceId().equals(saleReturnList.getProduceId())) {
                        saleReturnListOld.remove(j);
                        break;
                    }
                }
                //存在销售订单退货详细
                //更新
//                // 新建仓库子表的对象
//                WarehouseListDomain warehouseListDomain = new WarehouseListDomain();
//                warehouseListDomain.setWarehouseId(saleReturn.getWarehouseId());
//                warehouseListDomain.setProduceId(saleReturnListNew.get(i).getProduceId());
//                for (WarehouseListDto s : warehouseListDtos) {
//                    // 遍历仓库
//                    if (saleReturnListNew.get(i).getProduceId().equals(s.getProduceId())) {
//                        // 该产品存在，更新产品数量，产品数量 = 原库存数 - 该产品入库时增加的数量 + 新修改的入库数量
//                        warehouseListDomain.setProductNumber(s.getProductNumber() - saleReturnList.getSaleReturnNumber() + saleReturnListNew.get(i).getSaleReturnNumber());
//                        break; //终止循环
//                    }
//                }
//                warehouseMapper.updateWarehouseList(warehouseListDomain); // 更新仓库中该产品的数量
                saleReturnMapper.updateSaleReturnList(saleReturnListNew.get(i));
            } else {//新增
                saleReturnMapper.insertSaleReturnList(saleReturnListNew.get(i));
            }
        }
        // 说明有删除的对象
        for (int i = 0; i < saleReturnListOld.size(); i++) {
            // 新建仓库子表的对象
//            WarehouseListDomain warehouseListDomain = new WarehouseListDomain();
//            warehouseListDomain.setWarehouseId(saleReturn.getWarehouseId());
//            warehouseListDomain.setProduceId(saleReturnListOld.get(i).getProduceId());
//            for (WarehouseListDto s : warehouseListDtos) {
//                // 遍历仓库
//                if (saleReturnListOld.get(i).getProduceId().equals(s.getProduceId())) {
//                    // 删除时，产品库存数 = 原有库存 - 该产品入库时的库存数
//                    warehouseListDomain.setProductNumber(s.getProductNumber() - saleReturnListOld.get(i).getSaleReturnNumber());
//                    break; //终止循环
//                }
//            }
//            warehouseMapper.updateWarehouseList(warehouseListDomain); // 更新仓库中该产品的数量
            //最后将要删除的子标记录删除
            saleReturnMapper.deleteSaleReturnListById(saleReturnListOld.get(i).getSaleReturnId(), saleReturnListOld.get(i).getProduceId());
        }
        return 1;
    }

    /**
     * 更新销售订单退货删除标志位
     *
     * @param saleReturn 销售入库单据
     * @return
     */
    @Override
    public int updateSaleReturnDeleteFlag(SaleReturn saleReturn) {
        if (!StringUtils.isNotNull(saleReturnMapper.selectSaleReturnById(saleReturn.getSaleReturnId()))) {
            //不存在
            return 0;
        }
        List<SaleReturnList> saleReturnLists = saleReturn.getSaleReturnLists();// 被删除记录的所有子表记录
        List<WarehouseListDto> warehouseListDtos = warehouseMapper.selectWarehouseListListById(saleReturn.getWarehouseId()); // 查询该仓库的产品列表
        for (int i = 0; i < saleReturnLists.size(); i++) {
            // 新建仓库子表的对象
            WarehouseListDomain warehouseListDomain = new WarehouseListDomain();
            warehouseListDomain.setWarehouseId(saleReturn.getWarehouseId());
            warehouseListDomain.setProduceId(saleReturnLists.get(i).getProduceId());
            for (WarehouseListDto s : warehouseListDtos) {
                // 遍历仓库
                if (saleReturnLists.get(i).getProduceId().equals(s.getProduceId())) {
                    // 该产品存在，更新产品数量，产品数量 = 原库存数 - 该产品入库时增加的数量
                    warehouseListDomain.setProductNumber(s.getProductNumber() - saleReturnLists.get(i).getSaleReturnNumber());
                    break; //终止循环
                }
            }
            warehouseMapper.updateWarehouseList(warehouseListDomain); // 更新仓库中该产品的数量
        }
        return saleReturnMapper.updateSaleReturnDeleteFlag(saleReturn);
    }

    /**
     * 销售订单退货详细查询
     *
     * @param saleReturnList 销售入库产品信息
     * @return
     */
    @Override
    public List<SaleReturnList> selectSaleReturnListList(SaleReturnList saleReturnList) {
        return saleReturnMapper.selectSaleReturnListList(saleReturnList);
    }

    /**
     * 查询销售订单退货对应的所有产品详细
     *
     * @param saleReturnId 销售订单退货编号
     * @return
     */
    @Override
    public List<SaleReturnList> selectSaleReturnListListById(String saleReturnId) {
        return saleReturnMapper.selectSaleReturnListListById(saleReturnId);
    }

    /**
     * 查询销售订单退货对应的指定产品详细
     *
     * @param saleReturnId 销售订单退货编号
     * @param produceId    产品编号
     * @return
     */
    @Override
    public SaleReturnList selectSaleReturnListById(String saleReturnId, String produceId) {
        return saleReturnMapper.selectSaleReturnListById(saleReturnId, produceId);
    }

    /**
     * 新增销售订单退货时，获取对应的销售出库详细
     *
     * @param saleDeliveryId 销售订单出库编号
     * @return
     */
    @Override
    public List<SaleReturnList> getSaleDeliveryListForReturn(String saleDeliveryId) {
        List<SaleReturnList> saleReturnLists = saleReturnMapper.getSaleDeliveryListForReturn(saleDeliveryId);
        for (int i = 0; i < saleReturnLists.size(); ) {
            if (saleReturnLists.get(i).getSaleReturnNumber() <= 0) {
                saleReturnLists.remove(i);
            } else {
                i++;
            }
        }
        return saleReturnLists;
    }


}
